package com.ruoyi.comment.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.comment.mapper.TCommentMapper;
import com.ruoyi.comment.domain.TComment;
import com.ruoyi.comment.service.ITCommentService;

import javax.crypto.SecretKey;

/**
 * commentService业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-17
 */
@Service
public class TCommentServiceImpl implements ITCommentService 
{
    @Autowired
    private TCommentMapper tCommentMapper;

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询comment
     * 
     * @param id comment主键
     * @return comment
     */
    @Override
    public TComment selectTCommentById(Long id)
    {
        return tCommentMapper.selectTCommentById(id);
    }

    /**
     * 查询comment列表
     * 
     * @param tComment comment
     * @return comment
     */
    @Override
    public List<TComment> selectTCommentList(TComment tComment)
    {
        List<TComment> tComments = tCommentMapper.selectTCommentList(tComment);
        if(null != null && !tComments.isEmpty()){
            for (TComment comment : tComments) {
                Long commentator = comment.getCommentator();
                SysUser sysUser = iSysUserService.selectUserById(commentator);
                if (sysUser != null){
                    comment.setHeadImg(sysUser.getAvatar());
                    comment.setNickName(sysUser.getNickName());
                }
            }
        }
        return tComments;
    }

    /**
     * 新增comment
     * 
     * @param tComment comment
     * @return 结果
     */
    @Override
    public int insertTComment(TComment tComment)
    {
        tComment.setCreateTime(DateUtils.getNowDate());
        //评论人
        Long userId = SecurityUtils.getUserId();
        tComment.setCommentator(userId);
        return tCommentMapper.insertTComment(tComment);
    }

    /**
     * 修改comment
     * 
     * @param tComment comment
     * @return 结果
     */
    @Override
    public int updateTComment(TComment tComment)
    {
        return tCommentMapper.updateTComment(tComment);
    }

    /**
     * 批量删除comment
     * 
     * @param ids 需要删除的comment主键
     * @return 结果
     */
    @Override
    public int deleteTCommentByIds(Long[] ids)
    {
        return tCommentMapper.deleteTCommentByIds(ids);
    }

    /**
     * 删除comment信息
     * 
     * @param id comment主键
     * @return 结果
     */
    @Override
    public int deleteTCommentById(Long id)
    {
        return tCommentMapper.deleteTCommentById(id);
    }
}
